package entity;

import java.text.ParseException;
import java.util.Date;
import java.util.regex.Pattern;

public class Member {
	private MemberInfo memberInfo;
	private PersonalData personalData;

	public Member(String dataString) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, ParseException {
		String[] splitIntoTwo = dataString.split(";");

		personalData = new PersonalData(splitIntoTwo[0]);
		memberInfo = new MemberInfo(splitIntoTwo[1]);
	}

	public Member(String hkID, String lastName, String firstName, Date birthDay, String contactADDR, int contactNo, String gender, String memberID, Date date, int giftPtn, float bal, String status) {
		if (this.validateData(hkID, "[A-Z]{1}[0-9]{7}"))
			if (this.validateData(firstName, "^[a-zA-Z]+$"))
				if (this.validateData(lastName, "^[a-zA-Z]+$"))
					if (this.validateData(Integer.toString(contactNo), "[0-9]{8}"))
						if (this.validateData(gender, "[M|F]{1}"))
							if (this.validateData(memberID, "[0-9]{6}")) {
								// if (this.validateData(status, "[/^true$/|/^false$/]")) {
								this.memberInfo = new MemberInfo(memberID, date, giftPtn, bal, status);
								this.personalData = new PersonalData(hkID, lastName, firstName, birthDay, contactADDR, contactNo, gender);
							}
	}

	private boolean validateData(String data, String regEX) {
		if (Pattern.matches(regEX, data)) {
			return true;
		} else {
			return false;
		}

	}

	public Object getAttrVal(String c1) throws SecurityException, IllegalArgumentException, IllegalAccessException {
		if (this.memberInfo != null && this.personalData != null) {
			try {
				Object result = this.memberInfo.getValueByField(c1);
				return result;
			} catch (NoSuchFieldException e) {
				try {
					Object result = this.personalData.getValueByField(c1);
					return result;
				} catch (NoSuchFieldException e1) {
				}
			}
		}
		return null;

	}

	public void printContent() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, ParseException {
		if (this.personalData != null && this.memberInfo != null) {
			personalData.printContent();
			memberInfo.printContent();
			System.out.print("\n");
		}
	}

	public String getContentStr() throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException, ParseException {
		if (this.personalData != null && this.memberInfo != null) {
			String result = personalData.getContentStr();
			result += "; " + memberInfo.getContentStr();

			return result;
		} else {
			return null;
		}
	}

	public MemberInfo getMemberInfo() {
		return this.memberInfo;
	}

	public PersonalData getPersonalData() {
		return this.personalData;
	}
}
